<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2017-08-30
 * Time: 0:06
 */

namespace app\controllers;


use app\librarys\BaseController;
use app\models\SaleArticle;
use yii\data\Pagination;

class SaleArticleController extends BaseController
{
    /**
     * 营销宝典列表
     * @return string
     */
    public function actionSaleArticleList()
    {
        $request = \Yii::$app->request;
        $title = $request->get('title', '');
        $count = SaleArticle::getCount($title);
        $pagination = new Pagination(['totalCount' => $count]);
        $items = SaleArticle::getList($pagination->page, $pagination->pageSize, $title);
        return $this->render('sale-article-list', [
            'title' => $title,
            'items' => $items,
            'pagination' => $pagination
        ]);
    }

    /**
     * 营销宝典明细
     * @return string
     */
    public function actionSaleArticleDetail()
    {

        $id = \Yii::$app->request->post('id');
        $item = SaleArticle::getById($id);
        return $this->renderPartial('sale-article-detail',
            [
                'item' => $item
            ]
        );
    }

    /**
     * 保存营销宝典信息
     * @return string
     */
    public function actionSaleArticleSaveHandler()
    {
        $id = \Yii::$app->request->post('id');
        $title = \Yii::$app->request->post('title');
        $info = \Yii::$app->request->post('editorValue');

        //图片保存路径替换
        $info = SaleArticle::saveProductInfoImage($info);
        $reslut = SaleArticle::save($id, $title, $info);
        return $this->renderJson();
    }

    /**
     * 修改状态
     * @return string
     */
    public function actionSaleArticleModifyStatus()
    {
        $id = \Yii::$app->request->post('id');
        $result = SaleArticle::modifyStatus($id);
        return $this->renderJson(null, $result ? 0 : 1, $result ? '' : '修改失败');
    }


}